import { Byte } from '../../api/io/Byte';
import { HashMap } from '../../api/struct/HashMap';
import { LegionMember } from './LegionMember';
import { ApplyInfo } from './ApplyInfo';
import { LegionLog } from './LegionLog';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';
import { MathUtils } from '../../api/math/MathUtils';



/**军团信息属性枚举*/
export const enum E_Legion {
	/** 军团ID*/
	LEGION_ID = 0,
	/** 军团名*/
	LEGION_NAME = 1,
	/** 服务器id*/
	SERVER_ID = 2,
	/** 军团等级*/
	LEGION_LEVEL = 3,
	/** 军团经验*/
	LEGION_EXP = 4,
	/** 军团公告*/
	LEGION_NOTICE = 5,
	/** 军团长*/
	LEGION_LEADER = 6,
	/** 军团战力*/
	LEGION_COMBAT_POWER = 7,
	/** 军团旗帜*/
	LEGION_FLAG = 8,
	/** 成员列表*/
	MEMBERS = 9,
	/** 申请列表*/
	APPLICATIONS = 10,
	/** 军团日志*/
	LOGS = 11,
	/** 是否解散*/
	IS_DISMISS = 12,
	/**枚举数量*/
	EnumCount = 13
}


/**军团信息*/
export class Legion {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_LEGION_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_LEGION_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: Legion[] = [];

	/**创建入口*/
	public static create(json?: string): Legion {
		const c = this.POOL.pop() || new Legion();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/** 军团ID (int32)*/
	protected _legionId: number;
	/** 军团名*/
	protected _legionName: string;
	/** 服务器id (long)*/
	protected _serverId: number;
	/** 军团等级 (long)*/
	protected _legionLevel: number;
	/** 军团经验 (long)*/
	protected _legionExp: number;
	/** 军团公告*/
	protected _legionNotice: string;
	/** 军团长 (uint32)*/
	protected _legionLeader: number;
	/** 军团战力 (uint32)*/
	protected _legionCombatPower: number;
	/** 军团旗帜 (uint32)*/
	protected _legionFlag: number;
	/** 成员列表*/
	protected _members: HashMap<number, LegionMember> = null;
	/** 申请列表*/
	protected _applications: HashMap<number, ApplyInfo> = null;
	/** 军团日志*/
	protected _logs: LegionLog[] = [];
	/** 是否解散*/
	protected _isDismiss: boolean;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (Legion.POOL.indexOf(this) === -1) {
			Legion.POOL.push(this);
		}
	}

	/** 军团ID*/
	public get legionId() {
		return this._legionId;
	}


	/** 军团ID*/
	public set legionId(v: number) {
		this._legionId = v;
	}


	/** 军团名*/
	public get legionName() {
		return this._legionName;
	}


	/** 军团名*/
	public set legionName(v: string) {
		this._legionName = v;
	}


	/** 服务器id*/
	public get serverId() {
		return this._serverId;
	}


	/** 服务器id*/
	public set serverId(v: number) {
		this._serverId = v;
	}


	/** 军团等级*/
	public get legionLevel() {
		return this._legionLevel;
	}


	/** 军团等级*/
	public set legionLevel(v: number) {
		this._legionLevel = v;
	}


	/** 军团经验*/
	public get legionExp() {
		return this._legionExp;
	}


	/** 军团经验*/
	public set legionExp(v: number) {
		this._legionExp = v;
	}


	/** 军团公告*/
	public get legionNotice() {
		return this._legionNotice;
	}


	/** 军团公告*/
	public set legionNotice(v: string) {
		this._legionNotice = v;
	}


	/** 军团长*/
	public get legionLeader() {
		return this._legionLeader;
	}


	/** 军团长*/
	public set legionLeader(v: number) {
		this._legionLeader = v;
	}


	/** 军团战力*/
	public get legionCombatPower() {
		return this._legionCombatPower;
	}


	/** 军团战力*/
	public set legionCombatPower(v: number) {
		this._legionCombatPower = v;
	}


	/** 军团旗帜*/
	public get legionFlag() {
		return this._legionFlag;
	}


	/** 军团旗帜*/
	public set legionFlag(v: number) {
		this._legionFlag = v;
	}


	/**设置 成员列表*/
	public putMembers(k: number, v: LegionMember): LegionMember {
		const ov = this._members.put(k, v);
		return ov;
	}


	/**获取 成员列表*/
	public getMembers(k: number) {
		return this._members.get(k);
	}


	/**删除 成员列表*/
	public removeMembers(k: number, autoDestroy = false): LegionMember {
		const ov = this._members.remove(k);
		if (autoDestroy && ov) {
			ov.destroy();
		}
		return ov;
	}


	/**获取 成员列表映射表-键值(外部改变将导致出错，只能用于遍历)*/
	public getMembersKeys(): number[] {
		return this._members.getKeys();
	}


	/**获取 成员列表映射表-值(外部改变将导致出错，只能用于遍历)*/
	public getMembersValues(): LegionMember[] {
		return this._members.getValues();
	}


	/**清空 成员列表映射表*/
	public clearMembersMap(autoDestroy = false): void {
		const ks = this._members.getKeys();
		for (let i = ks.length - 1; i >= 0; i--) {
			this.removeMembers(ks[i], autoDestroy);
		}
	}


	/**设置 申请列表*/
	public putApplications(k: number, v: ApplyInfo): ApplyInfo {
		const ov = this._applications.put(k, v);
		return ov;
	}


	/**获取 申请列表*/
	public getApplications(k: number) {
		return this._applications.get(k);
	}


	/**删除 申请列表*/
	public removeApplications(k: number, autoDestroy = false): ApplyInfo {
		const ov = this._applications.remove(k);
		if (autoDestroy && ov) {
			ov.destroy();
		}
		return ov;
	}


	/**获取 申请列表映射表-键值(外部改变将导致出错，只能用于遍历)*/
	public getApplicationsKeys(): number[] {
		return this._applications.getKeys();
	}


	/**获取 申请列表映射表-值(外部改变将导致出错，只能用于遍历)*/
	public getApplicationsValues(): ApplyInfo[] {
		return this._applications.getValues();
	}


	/**清空 申请列表映射表*/
	public clearApplicationsMap(autoDestroy = false): void {
		const ks = this._applications.getKeys();
		for (let i = ks.length - 1; i >= 0; i--) {
			this.removeApplications(ks[i], autoDestroy);
		}
	}


	/**增加 军团日志*/
	public addLogs(v: LegionLog): void {
		if (this._logs.indexOf(v) === -1) {
			this._logs.push(v);
		}
	}

	/**删除 军团日志*/
	public removeLogs(v: LegionLog): void {
		const idx = this._logs.indexOf(v);
		if (idx >= 0) {
			this._logs.splice(idx, 1);
		}
	}

	/**获取 军团日志列表*/
	public getLogsList(): LegionLog[] {
		return this._logs;
	}

	/** 是否解散*/
	public get isDismiss() {
		return this._isDismiss;
	}


	/** 是否解散*/
	public set isDismiss(v: boolean) {
		this._isDismiss = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_Legion): any {
		switch (k) {
			case E_Legion.LEGION_ID: return this._legionId;
			case E_Legion.LEGION_NAME: return this._legionName;
			case E_Legion.SERVER_ID: return this._serverId;
			case E_Legion.LEGION_LEVEL: return this._legionLevel;
			case E_Legion.LEGION_EXP: return this._legionExp;
			case E_Legion.LEGION_NOTICE: return this._legionNotice;
			case E_Legion.LEGION_LEADER: return this._legionLeader;
			case E_Legion.LEGION_COMBAT_POWER: return this._legionCombatPower;
			case E_Legion.LEGION_FLAG: return this._legionFlag;
			case E_Legion.MEMBERS: return this._members;
			case E_Legion.APPLICATIONS: return this._applications;
			case E_Legion.LOGS: return this._logs;
			case E_Legion.IS_DISMISS: return this._isDismiss;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_Legion, v: any): void {
		switch (k) {
			case E_Legion.LEGION_ID: this._legionId = v; break;
			case E_Legion.LEGION_NAME: this._legionName = v; break;
			case E_Legion.SERVER_ID: this._serverId = v; break;
			case E_Legion.LEGION_LEVEL: this._legionLevel = v; break;
			case E_Legion.LEGION_EXP: this._legionExp = v; break;
			case E_Legion.LEGION_NOTICE: this._legionNotice = v; break;
			case E_Legion.LEGION_LEADER: this._legionLeader = v; break;
			case E_Legion.LEGION_COMBAT_POWER: this._legionCombatPower = v; break;
			case E_Legion.LEGION_FLAG: this._legionFlag = v; break;
			case E_Legion.MEMBERS: this._members = v; break;
			case E_Legion.APPLICATIONS: this._applications = v; break;
			case E_Legion.LOGS: this._logs = v; break;
			case E_Legion.IS_DISMISS: this._isDismiss = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		// 军团ID
		this._legionId = 0;
		// 军团名
		this._legionName = '';
		// 服务器id
		this._serverId = 0;
		// 军团等级
		this._legionLevel = 0;
		// 军团经验
		this._legionExp = 0;
		// 军团公告
		this._legionNotice = '';
		// 军团长
		this._legionLeader = 0;
		// 军团战力
		this._legionCombatPower = 0;
		// 军团旗帜
		this._legionFlag = 0;
		// 成员列表
		if (this._members) {
			const list = this._members.getValues();
			for (let i = 0, len = list.length; i < len; i++) {
				list[i].destroy();
			}
			this._members.clear();
		}
		else {
			this._members = new HashMap<number, LegionMember>();
		}
		// 申请列表
		if (this._applications) {
			const list = this._applications.getValues();
			for (let i = 0, len = list.length; i < len; i++) {
				list[i].destroy();
			}
			this._applications.clear();
		}
		else {
			this._applications = new HashMap<number, ApplyInfo>();
		}
		// 军团日志
		if (this._logs) {
			const list = this._logs;
			for (let i = 0, len = list.length; i < len; i++) {
				list[i].destroy();
			}
			this._logs.length = 0;
		}
		else {
			this._logs = [];
		}
		// 是否解散
		this._isDismiss = false;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._legionId) {
			json.legionId = this._legionId;
		}
		if (this._legionName) {
			json.legionName = this._legionName;
		}
		if (this._serverId) {
			json.serverId = this._serverId;
		}
		if (this._legionLevel) {
			json.legionLevel = this._legionLevel;
		}
		if (this._legionExp) {
			json.legionExp = this._legionExp;
		}
		if (this._legionNotice) {
			json.legionNotice = this._legionNotice;
		}
		if (this._legionLeader) {
			json.legionLeader = this._legionLeader;
		}
		if (this._legionCombatPower) {
			json.legionCombatPower = this._legionCombatPower;
		}
		if (this._legionFlag) {
			json.legionFlag = this._legionFlag;
		}
		if (this._members.size() > 0) {
			const ks = this._members.getKeys();
			const vs = this._members.getValues();
			const list: string[] = [];
			for (let i = 0, len = vs.length; i < len; i++) {
				list[i] = vs[i].stringify(false);
			}
			json.members = [ks, list];
		}
		if (this._applications.size() > 0) {
			const ks = this._applications.getKeys();
			const vs = this._applications.getValues();
			const list: string[] = [];
			for (let i = 0, len = vs.length; i < len; i++) {
				list[i] = vs[i].stringify(false);
			}
			json.applications = [ks, list];
		}
		if (this._logs) {
			const list: string[] = [];
			const vs = this._logs;
			for (let i = 0, len = vs.length; i < len; i++) {
				list[i] = vs[i].stringify(false);
			}
			json.logs = list;
		}
		if (this._isDismiss) {
			json.isDismiss = this._isDismiss;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.legionId) {
			this._legionId = json.legionId;
		}
		if (json.legionName) {
			this._legionName = json.legionName;
		}
		if (json.serverId) {
			this._serverId = json.serverId;
		}
		if (json.legionLevel) {
			this._legionLevel = json.legionLevel;
		}
		if (json.legionExp) {
			this._legionExp = json.legionExp;
		}
		if (json.legionNotice) {
			this._legionNotice = json.legionNotice;
		}
		if (json.legionLeader) {
			this._legionLeader = json.legionLeader;
		}
		if (json.legionCombatPower) {
			this._legionCombatPower = json.legionCombatPower;
		}
		if (json.legionFlag) {
			this._legionFlag = json.legionFlag;
		}
		if (json.members) {
			const ks: number[] = json.members[0] || (json.members.getKeys ? json.members.getKeys() : null);	//对象可能是json或者结构体
			const vs: string[] = json.members[1] || (json.members.getValues ? json.members.getValues() : null);
			if (ks && vs && ks.length === vs.length) {
				for (let i = 0, len = vs.length; i < len; i++) {
					this._members.put(ks[i], LegionMember.create(vs[i]));
				}
			}
		}
		if (json.applications) {
			const ks: number[] = json.applications[0] || (json.applications.getKeys ? json.applications.getKeys() : null);	//对象可能是json或者结构体
			const vs: string[] = json.applications[1] || (json.applications.getValues ? json.applications.getValues() : null);
			if (ks && vs && ks.length === vs.length) {
				for (let i = 0, len = vs.length; i < len; i++) {
					this._applications.put(ks[i], ApplyInfo.create(vs[i]));
				}
			}
		}
		if (json.logs) {
			const list = json.logs;
			for (let i = 0, len = list.length; i < len; i++) {
				this._logs.push(LegionLog.create(list[i]));
			}
		}
		if (json.isDismiss) {
			this._isDismiss = json.isDismiss;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeInt32(this._legionId);
		byte.writeString(this._legionName);
		byte.writeLongNumber(this._serverId);
		byte.writeLongNumber(this._legionLevel);
		byte.writeLongNumber(this._legionExp);
		byte.writeString(this._legionNotice);
		byte.writeUint32(this._legionLeader);
		byte.writeUint32(this._legionCombatPower);
		byte.writeUint32(this._legionFlag);
		// 成员列表
		{
			const ks = this._members.getKeys();
			const vs = this._members.getValues();
			const len = ks.length;
			byte.writeUint16(len);
			for (let i = 0; i < len; i++) {
				byte.writeUint32(ks[i]);
				vs[i].toBytes(byte);
			}
		}
		// 申请列表
		{
			const ks = this._applications.getKeys();
			const vs = this._applications.getValues();
			const len = ks.length;
			byte.writeUint16(len);
			for (let i = 0; i < len; i++) {
				byte.writeUint32(ks[i]);
				vs[i].toBytes(byte);
			}
		}
		// 军团日志
		{
			const list = this._logs;
			if (list) {
				byte.writeUint32(list.length);
				for (let i = 0, len = list.length; i < len; i++) {
					list[i].toBytes(byte);
				}
			}
			else {
				byte.writeUint32(0);
			}
		}
		byte.writeBool(this._isDismiss);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._legionId = byte.readInt32();
		this._legionName = byte.readString();
		this._serverId = byte.readLongNumber();
		this._legionLevel = byte.readLongNumber();
		this._legionExp = byte.readLongNumber();
		this._legionNotice = byte.readString();
		this._legionLeader = byte.readUint32();
		this._legionCombatPower = byte.readUint32();
		this._legionFlag = byte.readUint32();
		// 成员列表
		{
			const len = byte.readUint16();
			for (let i = 0; i < len; i++) {
				const k = byte.readUint32();
				const v = LegionMember.create();
				v.fromBytes(byte);
				this._members.put(k, v);
			}
		}
		// 申请列表
		{
			const len = byte.readUint16();
			for (let i = 0; i < len; i++) {
				const k = byte.readUint32();
				const v = ApplyInfo.create();
				v.fromBytes(byte);
				this._applications.put(k, v);
			}
		}
		// 军团日志
		{
			const len = byte.readUint32();
			for (let i = 0; i < len; i++) {
				const cv = LegionLog.create();
				cv.fromBytes(byte);
				this._logs[i] = cv;
			}
		}
		this._isDismiss = byte.readBool();
		return this;
	}


	/**克隆*/
	public clone(): Legion {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = Legion.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = Legion.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			// 军团ID
			if (idx === E_Legion.LEGION_ID) {
				const oldv = this._legionId;
				this._legionId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionId, oldv);
				i += 2;
				continue;
			}
			// 军团名
			if (idx === E_Legion.LEGION_NAME) {
				const oldv = this._legionName;
				this._legionName = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionName, oldv);
				i += 2;
				continue;
			}
			// 服务器id
			if (idx === E_Legion.SERVER_ID) {
				const oldv = this._serverId;
				this._serverId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._serverId, oldv);
				i += 2;
				continue;
			}
			// 军团等级
			if (idx === E_Legion.LEGION_LEVEL) {
				const oldv = this._legionLevel;
				this._legionLevel = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionLevel, oldv);
				i += 2;
				continue;
			}
			// 军团经验
			if (idx === E_Legion.LEGION_EXP) {
				const oldv = this._legionExp;
				this._legionExp = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionExp, oldv);
				i += 2;
				continue;
			}
			// 军团公告
			if (idx === E_Legion.LEGION_NOTICE) {
				const oldv = this._legionNotice;
				this._legionNotice = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionNotice, oldv);
				i += 2;
				continue;
			}
			// 军团长
			if (idx === E_Legion.LEGION_LEADER) {
				const oldv = this._legionLeader;
				this._legionLeader = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionLeader, oldv);
				i += 2;
				continue;
			}
			// 军团战力
			if (idx === E_Legion.LEGION_COMBAT_POWER) {
				const oldv = this._legionCombatPower;
				this._legionCombatPower = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionCombatPower, oldv);
				i += 2;
				continue;
			}
			// 军团旗帜
			if (idx === E_Legion.LEGION_FLAG) {
				const oldv = this._legionFlag;
				this._legionFlag = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionFlag, oldv);
				i += 2;
				continue;
			}

			// 成员列表
			if (idx === E_Legion.MEMBERS) {
				const k: number = us[i + 1];
				const ctrl: number = us[i + 2];
				if (ctrl === 0) {
					const oldv = this.removeMembers(k);
					if (oldv) {
						oldv.destroy()
					}
				}
				else if (ctrl === 1) {
					const newv = LegionMember.create(us[i + 3]);
					const oldv = this.putMembers(k, newv);
					if (oldv) {
						oldv.destroy()
					}
				}
				else {
					const curv = this.getMembers(k);
					if (curv) {
						curv.processUpdate(us[i + 3]);
					}
					else {
						console.error('Legion LegionMember is not exist when update! k = ' + k);
					}
				}
				IAsync.dispatch(eventID, idx, k, ctrl);
				i += ctrl === 0 ? 3 : 4;
				continue;
			}

			// 申请列表
			if (idx === E_Legion.APPLICATIONS) {
				const k: number = us[i + 1];
				const ctrl: number = us[i + 2];
				if (ctrl === 0) {
					const oldv = this.removeApplications(k);
					if (oldv) {
						oldv.destroy()
					}
				}
				else if (ctrl === 1) {
					const newv = ApplyInfo.create(us[i + 3]);
					const oldv = this.putApplications(k, newv);
					if (oldv) {
						oldv.destroy()
					}
				}
				else {
					const curv = this.getApplications(k);
					if (curv) {
						curv.processUpdate(us[i + 3]);
					}
					else {
						console.error('Legion ApplyInfo is not exist when update! k = ' + k);
					}
				}
				IAsync.dispatch(eventID, idx, k, ctrl);
				i += ctrl === 0 ? 3 : 4;
				continue;
			}
			// 军团日志
			if (idx === E_Legion.LOGS) {
				const c: number = us[i + 1];
				const v = LegionLog.create(us[i + 2]);
				IAsync.dispatch(eventID, idx, c, v);
				i += 3;
				continue;
			}
			// 是否解散
			if (idx === E_Legion.IS_DISMISS) {
				const oldv = this._isDismiss;
				this._isDismiss = us[i + 1];
				IAsync.dispatch(eventID, idx, this._isDismiss, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in Legion with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: Legion): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._legionId !== other._legionId) {
			updates.push(E_Legion.LEGION_ID, this._legionId);
		}
		if (this._legionName !== other._legionName) {
			updates.push(E_Legion.LEGION_NAME, this._legionName);
		}
		if (this._serverId !== other._serverId) {
			updates.push(E_Legion.SERVER_ID, this._serverId);
		}
		if (this._legionLevel !== other._legionLevel) {
			updates.push(E_Legion.LEGION_LEVEL, this._legionLevel);
		}
		if (this._legionExp !== other._legionExp) {
			updates.push(E_Legion.LEGION_EXP, this._legionExp);
		}
		if (this._legionNotice !== other._legionNotice) {
			updates.push(E_Legion.LEGION_NOTICE, this._legionNotice);
		}
		if (this._legionLeader !== other._legionLeader) {
			updates.push(E_Legion.LEGION_LEADER, this._legionLeader);
		}
		if (this._legionCombatPower !== other._legionCombatPower) {
			updates.push(E_Legion.LEGION_COMBAT_POWER, this._legionCombatPower);
		}
		if (this._legionFlag !== other._legionFlag) {
			updates.push(E_Legion.LEGION_FLAG, this._legionFlag);
		}

		// 成员列表
		{
			const idx = E_Legion.MEMBERS;
			const ks1 = this._members.getKeys();
			const ks2 = other._members.getKeys();
			const rs = MathUtils.intersectSortedArray(ks1, ks2);
			if (rs[0].length > 0) {
				const list = rs[0];
				for (let i = 0, len = list.length; i < len; i++) {
					const k = list[i];
					const newv = this.getMembers(k);
					const oldv = other.getMembers(k);
					const ui = newv.getCompareInfo(oldv);
					if (ui && ui.length > 0) {
						updates.push(idx, k, 2, ui);
					}
				}
			}
			if (rs[1].length > 0) {
				const list = rs[1];
				for (let i = 0, len = list.length; i < len; i++) {
					const k = list[i];
					const newv = this.getMembers(k);
					updates.push(idx, k, 1, newv.stringify(false));
				}
			}
			if (rs[2].length > 0) {
				const list = rs[2];
				for (let i = 0, len = list.length; i < len; i++) {
					updates.push(idx, list[i], 0);
				}
			}
		}

		// 申请列表
		{
			const idx = E_Legion.APPLICATIONS;
			const ks1 = this._applications.getKeys();
			const ks2 = other._applications.getKeys();
			const rs = MathUtils.intersectSortedArray(ks1, ks2);
			if (rs[0].length > 0) {
				const list = rs[0];
				for (let i = 0, len = list.length; i < len; i++) {
					const k = list[i];
					const newv = this.getApplications(k);
					const oldv = other.getApplications(k);
					const ui = newv.getCompareInfo(oldv);
					if (ui && ui.length > 0) {
						updates.push(idx, k, 2, ui);
					}
				}
			}
			if (rs[1].length > 0) {
				const list = rs[1];
				for (let i = 0, len = list.length; i < len; i++) {
					const k = list[i];
					const newv = this.getApplications(k);
					updates.push(idx, k, 1, newv.stringify(false));
				}
			}
			if (rs[2].length > 0) {
				const list = rs[2];
				for (let i = 0, len = list.length; i < len; i++) {
					updates.push(idx, list[i], 0);
				}
			}
		}

		// 军团日志
		{
			const idx = E_Legion.LOGS;
			const rs = MathUtils.intersectSortedArray(this._logs, other._logs);
			if (rs[0].length > 0) {
				const list = rs[0];
				for (let i = 0, len = list.length; i < len; i++) {
					const newv = list[i];
					updates.push(idx, 1, newv.stringify(false));
				}
			}
			if (rs[1].length > 0) {
				const list = rs[1];
				for (let i = 0, len = list.length; i < len; i++) {
					const newv = list[i];
					updates.push(idx, 1, newv.stringify(false));
				}
			}
			if (rs[2].length > 0) {
				const list = rs[2];
				for (let i = 0, len = list.length; i < len; i++) {
					const oldv = list[i];
					updates.push(idx, 0, oldv.getV(0));
				}
			}
		}
		if (this._isDismiss !== other._isDismiss) {
			updates.push(E_Legion.IS_DISMISS, this._isDismiss);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(Legion.TOTAL_EVENT_ID, this.getV(0));
	}

}
